<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
    :confirmLoading="confirmLoading"
    :okText="$t('保存')"
    :cancelText="$t('关闭')"
  >
    <a-form-model ref="form" :model="model" :rules="validatorRules">
      <a-row>
        <a-col :span="24">
          <a-form-model-item :label="$t('设备编码')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceId">
            <div>{{ model.deviceId }}</div>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item :label="$t('投币外设')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inputPeri">
            <j-dict-select-tag
              type="list"
              v-model="model.inputPeri"
              dictCode="yn"
              :placeholder="$t('请选择')"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item :label="$t('退票外设')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="outputPeri">
            <j-dict-select-tag
              type="list"
              v-model="model.outputPeri"
              dictCode="yn"
              :placeholder="$t('请选择')"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'

export default {
  name: 'DevicePeriCertModal',
  components: {},
  data() {
    return {
      title: '',
      width: 800,
      visible: false,
      disableSubmit: false,
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      model: {
        type: '',
      },
      validatorRules: {
        inputPeri: [{ required: true, message: this.$t('请选择') }],
        outputPeri: [{ required: true, message: this.$t('请选择') }],
      },
      url: {
        add: '/device/bisDeviceInfo/add',
        edit: '/device/bisDeviceInfo/periCert',
        queryById: '/device/bisDeviceInfo/queryById',
      },
      gameList: [],
      outputType: '',
      goodsList: [],
    }
  },
  methods: {
    add() {
      this.visible = true
      this.edit()
    },
    edit(record) {
      console.log(record.type)
      this.visible = true
      this.gameList = []
      this.goodsList = []
      this.outputType = ''
      this.getGameList()
      this.getGoodsList()
      if ((record.type == 'EF310004' || record.type == 'EF300004') && record.bisDeviceGoodsList.length > 0) {
        record.goodsId = record.bisDeviceGoodsList[0].goodsId
      }
      this.model = Object.assign({}, record)
      if (
        this.model.type == 'ATM08' ||
        this.model.type == 'YJ_D06' ||
        this.model.type == 'EF310004' ||
        this.model.type == 'EF300004' ||
        this.model.type == 'P09'
      ) {
        let list = [],
          listTwo = [],
          listThree = []
        this.model.deviceImageList.forEach((col) => {
          if (col.type == 1) {
            list.push(col.path)
          }
          if (this.model.type == 'ATM08' && col.type == 2) {
            listTwo.push(col.path)
          }
          if (
            (this.model.type == 'ATM08' || this.model.type == 'YJ_D06' || this.model.type == 'P09') &&
            col.type == 3
          ) {
            listThree.push(col.path)
          }
        })
        this.model.pictureTop = list.join(',')
        if (this.model.type == 'ATM08') {
          this.model.pictureBot = listTwo.join(',')
        }
        if (this.model.type == 'ATM08' || this.model.type == 'YJ_D06' || this.model.type == 'P09') {
          this.model.pictureAds = listThree.join(',')
        }
      }
    },
    handleChange(e) {
      this.gameList.forEach((item) => {
        if (item.id == e) {
          this.outputType = item.outputType
        }
      })
    },
    getGameList(info) {
      let that = this
      that.gameList = []
      let querydata = {}
      querydata.pageIndex = 1
      querydata.pageSize = 999
      querydata.gameInfo = info
      getAction('/game/bisGame/list', querydata).then((res) => {
        if (res.success) {
          that.gameList = res.result.records
          if (
            that.model.type.indexOf('HZ') == 0 ||
            that.model.type.indexOf('T') == 0 ||
            that.model.type.indexOf('YP') > -1 ||
            that.model.type == 'EF100002'
          ) {
            that.handleChange(that.model.game)
          }
        }
      })
    },
    handleOk() {
      let that = this
      // 触发表单验证
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!that.model.id) {
            httpurl += that.url.add
            method = 'post'
          } else {
            httpurl += that.url.edit
            method = 'put'
          }
          let pictureTop = []
          let pictureBot = []
          let pictureAds = []
          if (that.model.pictureTop) {
            let list = []
            list = that.model.pictureTop.split(',')
            list.forEach((lt) => {
              let obj = {}
              obj.path = lt
              obj.type = 1
              pictureTop.push(obj)
            })
          }
          if (that.model.pictureBot) {
            let list = []
            list = that.model.pictureBot.split(',')
            list.forEach((lt) => {
              let obj = {}
              obj.path = lt
              obj.type = 2
              pictureBot.push(obj)
            })
          }
          if (that.model.pictureAds) {
            let list = []
            list = that.model.pictureAds.split(',')
            list.forEach((lt) => {
              let obj = {}
              obj.path = lt
              obj.type = 3
              pictureAds.push(obj)
            })
          }
          that.model.deviceImageList = pictureTop.concat(pictureBot, pictureAds)
          if (that.model.goodsId) {
            that.model.bisDeviceGoodsList = [{ goodsId: that.model.goodsId }]
          }
          httpAction(httpurl, that.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
    },
    getGoodsList(value) {
      let that = this
      let querydata = {}
      querydata.pageNo = 1
      querydata.pageSize = 200
      querydata.goodsInfo = value
      querydata.form = '1'
      getAction('/goods/bisGoodsInfor/list', querydata).then((res) => {
        if (res.success) {
          that.goodsList = res.result.records
        }
      })
    },
    changeGoods() {
      this.getGoodsList()
    },
  },
}
</script>